<?php

namespace App\Http\Controllers;

use App\SiteInfo;
use Illuminate\Http\Request;

class SiteInfoController extends Controller
{
    public function edit(Request $request){
        $id = '99';//id默认是9999

        if(!$_POST){
            return view('System.siteinfo', [
                'info' => SiteInfo::find($id)
            ]);
        }

        $company_name = $request->filled('company_name') ? $request->company_name : '';
        $company_addr = $request->filled('company_addr') ? $request->company_addr : '';
        $tel = $request->filled('tel') ? $request->tel : '';
        $phone = $request->filled('phone') ? $request['phone'] : '';
        $fax = $request->filled('fax') ? $request['fax'] : '';
        $email = $request->filled('email') ? $request['email'] : '';
        $contact = $request->filled('contact') ? $request['contact'] : '';
        $qq = $request->filled('qq') ? $request['qq'] : '';
        $wechat = $request->filled('wechat') ? $request['wechat'] : '';
        $zipcode = $request->filled('zipcode') ? $request['zipcode'] : '';
        $record_number = $request->filled('record_number') ? $request['record_number'] : '';
        $site_url = $request->filled('site_url') ? $request['site_url'] : '';
        $detail = $request->filled('detail') ? parseUeditorImage($request['detail']) : '';

        SiteInfo::updateOrCreate(
            ['id' => $id],
            [
                'company_name' => $company_name,
                'company_addr' => $company_addr,
                'tel' => $tel,
                'phone' => $phone,
                'fax' => $fax,
                'email' => $email,
                'contact' => $contact,
                'qq' => $qq,
                'wechat' => $wechat,
                'zipcode' => $zipcode,
                'record_number' => $record_number,
                'site_url' => $site_url,
                'detail' => $detail
            ]
        );

        return json_encode([
            'code' => 1,
            'msg' => 'success'
        ]);
    }

    //=====================================
    //          [API  Function]
    //=====================================

    public function info(){
        return json_encode([
            'code' => 1,
            'data' => SiteInfo::find(99)
        ]);
    }
}
